import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

/**
 *
 * @author tub
 */
public class MusicFrame extends JFrame
{
    public MusicFrame ()
    {
        setTitle("MusicBase");
        setSize(1000, 600);

        Label label = new Label();
        Performer perf = new Performer();

        MusicEntity[] ent = {label, perf};
        explorer = new ExplorePanel();
        search = new SearchPanel(ent, explorer);

        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        
        GridBagConstraints searchConstr = new GridBagConstraints();
        searchConstr.weightx = 100;
        searchConstr.weighty = 100;
        searchConstr.gridx = 0;
        searchConstr.gridy = 0;
        searchConstr.gridheight = 1;
        searchConstr.gridwidth = 1;
        searchConstr.fill = GridBagConstraints.BOTH;
        searchConstr.insets = new Insets(5, 10, 5, 10);
        
        GridBagConstraints explorerConstr = new GridBagConstraints();
        explorerConstr.weightx = 100;
        explorerConstr.weighty = 100;
        explorerConstr.gridx = 1;
        explorerConstr.gridy = 0;
        explorerConstr.gridheight = 1;
        explorerConstr.gridwidth = 2;
        explorerConstr.fill = GridBagConstraints.BOTH;
        explorerConstr.insets = new Insets(5, 0, 5, 10);

        panel.add(search, searchConstr);
        panel.add(explorer, explorerConstr);

        
        setLayout(new BorderLayout());
        JToolBar toolbar = new JToolBar();

        JButton addRow = new JButton();
        add(panel);
        add(createToolBar(), BorderLayout.NORTH);

        /*ResultSet result = label.selectTitle("1");
        BufferedWriter out = new BufferedWriter(new FileWriter("report.txt"));

        while (result.next())
        {
            out.write((result.getString(1)) + ", " + result.getString(2));
            out.newLine();
        }
        out.close();*/
    }

    public static Connection getConnection() throws IOException, SQLException
    {
        Locale.setDefault(Locale.ENGLISH);
        DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
        return DriverManager.getConnection ("jdbc:oracle:thin:@localhost:1521:XE", "music", "music");
    }

    public JToolBar createToolBar()
    {
        JToolBar toolbar = new JToolBar();

        JButton addRow = new JButton("Add row");
        addRow.addActionListener(new AbstractAction() {

            public void actionPerformed(ActionEvent e)
            {
                try
                {
                    explorer.addRow();  
                } 
                catch (SQLException ex)
                {
                    ex.printStackTrace();
                }
                catch (IOException ex)
                {
                    ex.printStackTrace();
                }
            }
        });
        
        toolbar.add(addRow);
        toolbar.setOpaque(true);

        return toolbar;
    }

    private ExplorePanel explorer;
    private SearchPanel search;
}

